package com.durian.base.rxhttp.parse

import okhttp3.Response
import org.json.JSONArray
import java.io.IOException

/**
 * 说明：SimpleListParser
 * <p/>
 * 作者：Fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2020/04/27 19:51
 * <p/>
 * 版本：version 1.0
 */
open class DefaultListParser<T> : DefaultParser<MutableList<T>>() {

    @Throws(IOException::class)
    override fun onParse(response: Response): MutableList<T> {
        checkResponse(response)
        return convertList(response.body?.string()?:"")
    }

    open fun convertList(result: String): MutableList<T>{
        val jsonArray = JSONArray(result)
        val list = mutableListOf<T>()
        (0 until jsonArray.length()).forEach {
            list.add(GsonConverter.gson.fromJson(jsonArray[it].toString(), mType))
        }
        return list
    }
}